package uk.co.richardwkirk.parkrun.web.parsers;

import org.jsoup.nodes.Element;

public class LatestResultsParser extends ParkrunWebPageParser {

	public LatestResultsParser(String summaryPageContent) throws ParkrunDataParseException {
		super(summaryPageContent);
	}

	public int getLatestParkrunNumber() throws ParkrunDataParseException {
		// Create a filter to extract the table node we are interested in
		Element h2Node = getFirstMatchingNode("h2");
		
		// Get the name from the filter
		if (h2Node != null) {
			try {
				String titleString = h2Node.text();
				String numberAtStart = titleString.substring(titleString.indexOf("#") + 1).trim();
				String numberOnly = numberAtStart.substring(0, numberAtStart.indexOf(" "));
				return Integer.parseInt(numberOnly);
			}
			catch (Exception e) {
				throw new ParkrunDataParseException("Could not get parkrun number from results.", e);
			}
		}
		else {
			return 0;
		}
	}
	
	
}
